
--
--Version 1.8
--

--
--winxp
--linkoptions { "/Zc:threadSafeInit-"}
--
--
--PmGenSln
--PmGenPmx
--PmGenSubDir PmGenSubDir4 PmGenSubDir5

----PmGenApp(the_name,apptype,charset,mfcbuildflags,staticcrt,winver)
----the_name
----apptype       'WindowedApp','ConsoleApp','SharedLib','StaticLib'
----charset       'unicode' 'mbcs'
----mfcbuildflags 'MFC-Shared' 'MFC-Shared-Ext' 'MFC-Static' 'MFC-None'
----staticcrt     'ON' 'OFF'
----winver        "winxp","winvista","win7","wincur"


--PmGenApp
--PmGenApp5
--PmGenApp3

--PmGenNone

--PmFileIsExists

--PmGenDef
--PmGenInc
--PmGenFile

--PmGenLink
--PmGenLinkDt
--PmGenLinkDir

--PmCopyFile
--PmCopyFilesBin
--PmCopyFilesLib
--PmCopyFilesXF


--Init Glob Var
---- g_buildsp_loction_buildir  true false
---- g_dbg_suffix_d true false
---- g_the_top_dir
---- g_the_top_w64  true false
---- g_the_pmver    3    5
---- g_clpx         E    K       T       A       B       C       X        P       Y       Z
---- g_vcver        6    8       9       10      11      12      14       15      16      17
-----               vs6  vs2005  vs2008  vs2010  vs2012  vs2013  vs2015   vs2017  vs2019  vs2022

--VS Tool Set
--premake4
--$(SolutionDir)\..\zztool\premake4.exe
--vs2005 --file=premake5.lua
--$(SolutionDir)\..\


if g_the_top_dir then
    return
end


g_the_pmver=0
if target~=nil then
    g_the_pmver=3
end
if _ACTION==nil then
    g_the_pmver=3
end
if _ACTION~=nil then
    g_the_pmver=5
end


--print('target... '.. target)
--print('_ACTION... '.. _ACTION)

if not g_the_top_dir then
    if g_the_pmver==5 then
        g_the_top_dir = path.translate(path.getabsolute("."), "\\")
    end
    if g_the_pmver==3 then
        g_the_top_dir=os.getcwd()
    end
end

print('load pmgenpj ...'.. g_the_pmver)

g_vcver=14
g_clpx=""
g_libusefn = {}

function PmGenMod(name)
    if g_libusefn[name..'_use_fn'] ~=nil then
        g_libusefn[name..'_use_fn']()
    end
end

if g_the_pmver==3 then
    if (target == "vs6" )then
        g_clpx="E"
        g_vcver=6
    end
    if (target == "vs2008" )then
        g_clpx="T"
        g_vcver=9
    end
    if (target == "vs2005" )then
        g_clpx="K"
        g_vcver=8
    end
end

if g_the_pmver==5 then
    if _ACTION == "vs2005" then
        g_clpx="K"
        g_vcver=8
    elseif _ACTION == "vs2008" then
        g_clpx="T"
        g_vcver=9
    elseif _ACTION == "vs2010" then
        g_clpx="A"
        g_vcver=10
    elseif _ACTION == "vs2012" then
        g_clpx="B"
        g_vcver=11
    elseif _ACTION == "vs2013" then
        g_clpx="C"
        g_vcver=12
    elseif _ACTION == "vs2015" then
        g_clpx="X"
        g_vcver=14
    elseif _ACTION == "vs2017" then
        g_clpx="P"
        g_vcver=15
    elseif _ACTION == "vs2019" then
        g_clpx="Y"
        g_vcver=16
    elseif _ACTION == "vs2022" then
        g_clpx="Z"
        g_vcver=17
    end
end


if g_the_pmver==3 then
    function location(dir)
    end
end

function PmFileIsExists(name)
    if g_the_pmver==3 then
       local f=io.open(name,"r")
       if f~=nil then
            io.close(f)
            return true
        else
            return false
        end
        return false
    end
    return os.isfile(name)
end


function PmGenSln(the_name)
    if g_the_pmver==3 then
        PmGenSln3(the_name)
    end
    if g_the_pmver==5 then
        if g_the_top_w64~=nil and g_the_top_w64==true then
            PmGenSln5_w64(the_name)
        else
            PmGenSln5(the_name)
        end

    end
end

function PmGenSln3(the_name)
    project.name = the_name
end

function PmGenSln5(the_name)
    workspace (the_name)
        configurations {'Debug', 'Release'}

        platforms {"Win32"}
        g_the_top_w64=false

		if g_buildsp_loction_buildir==true then
			location ('_Build'..g_clpx)
		else
			location ('./')
		end

        objdir (g_the_top_dir.."/Obj"..g_clpx.."/%{prj.name}/%{cfg.buildcfg}")

        filter {"platforms:Win32"}
            targetdir (g_the_top_dir.."/Bin"..g_clpx.."/%{cfg.buildcfg}")
            implibdir (g_the_top_dir.."/Lib"..g_clpx.."/%{cfg.buildcfg}")
            debugdir  ("$(TargetDir)")
        filter {}

        filter {'kind:StaticLib',"platforms:Win32"}
            targetdir (g_the_top_dir.."/Lib"..g_clpx.."/%{cfg.buildcfg}")
        filter {}

        filter {"configurations:Debug"}
            defines { "DEBUG","_DEBUG"}
            symbols "On"
        filter {"configurations:Release"}
            defines { "NDEBUG" }
            optimize "On"
            symbols "On"
        filter {}

        if g_dbg_suffix_d then
            filter {"configurations:Debug"}
                targetsuffix "d"
                implibsuffix "d"
            filter {}
        end
end

function PmGenSln5_w64(the_name)
    workspace (the_name)
        configurations {'Debug', 'Release'}

		if g_buildsp_loction_buildir==true then
			location ('_Build'..g_clpx)
		else
			location ('./')
		end
		
        g_the_top_w64=true
        platforms {"Win32","x64"}


        filter "platforms:Win32"
            architecture "x86"
        filter "platforms:x64"
            architecture "x86_64"
        filter {}

        filter {"platforms:Win32"}
            targetdir ("Bin"..g_clpx.."/%{cfg.buildcfg}")
            implibdir ("Lib"..g_clpx.."/%{cfg.buildcfg}")
            objdir ("Obj"..g_clpx.."/%{prj.name}/%{cfg.buildcfg}")
            debugdir  ("$(TargetDir)")
        filter {"platforms:x64"}
            targetdir ("Bin"..g_clpx.."/%{cfg.buildcfg}x64")
            implibdir ("Lib"..g_clpx.."/%{cfg.buildcfg}x64")
            objdir ("Obj"..g_clpx.."/%{prj.name}x64/%{cfg.buildcfg}x64")
            debugdir  ("$(TargetDir)")
        filter {}

        filter {'kind:StaticLib',"platforms:Win32"}
            targetdir ("Lib"..g_clpx.."/%{cfg.buildcfg}")
        filter {'kind:StaticLib',"platforms:x64"}
            targetdir ("Lib"..g_clpx.."/%{cfg.buildcfg}x64")
        filter {}

        filter {"configurations:Debug"}
            defines { "DEBUG","_DEBUG"}
            symbols "On"
        filter {"configurations:Release"}
            defines { "NDEBUG" }
            optimize "On"
            symbols "On"
        filter {}

        if g_dbg_suffix_d then
            filter {"configurations:Debug"}
                targetsuffix "d"
                implibsuffix "d"
            filter {}
        end
end

function PmGenPmx(tddir)
    if g_the_pmver==5 then
    project ("_Pmake")
        if tddir~=nil then
			location (tddir)
        else
			if g_buildsp_loction_buildir==true then
				location ('_Build'..g_clpx)
			else
				location ('./')
			end
		end

        kind "Makefile"

        if g_vcver==12 then
            toolset "v120"
        end

        files {'*.lua'}

        vpaths { ['*'] = {path.translate(path.getabsolute("."), "\\")}}

        if g_vcver < 10 then
            rebuildcommands {
                "if exist premake.lua (premake5 ".._ACTION.."  --file=premake.lua)",
                "if exist premake5.lua (premake5 ".._ACTION.."  --file=premake5.lua)"
            }
        else
            rebuildcommands {
                "if exist premake.lua (premake5 ".._ACTION.."  --file=premake.lua)",
                "if exist premake5.lua (premake5 ".._ACTION.."  --file=premake5.lua)"
            }
        end
    end
end


function PmGenPmxReBuld()
    if g_the_pmver==5 then
        if g_vcver > 10 then
            rebuildcommands {
                "if exist premake.lua (premake5 ".._ACTION.."  --file=premake.lua)",
                "if exist premake5.lua (premake5 ".._ACTION.."  --file=premake5.lua)"
            }
         end
    end
end

function PmGenSubDir(TDir)
    if g_the_pmver==3 then
        if PmFileIsExists(TDir.."/premake.lua") then
            dopackage(TDir.."/premake.lua")
        end
    end
    if g_the_pmver==5 then
        if os.isfile(TDir.."/premake.lua") then
            dofile(TDir.."/premake.lua")
        elseif os.isfile(TDir.."/premake5.lua") then
            dofile(TDir.."/premake5.lua")
        else
            print('Error Not premake.lua or premake5.lua TheDir: '..TDir)
        end
    end
end

function PmGenSubDir4(TDir)
    if (g_vcver>=8) and (g_vcver<10) then
        if os.isfile(TDir.."/premake.lua") then
            dofile(TDir.."/premake.lua")
        elseif os.isfile(TDir.."/premake5.lua") then
            dofile(TDir.."/premake5.lua")
        else
            print('Error Not premake.lua or premake5.lua TheDir: '..TDir)
        end
    end
end

function PmGenSubDir5(TDir)
    if g_vcver>=10 then
        if os.isfile(TDir.."/premake.lua") then
            dofile(TDir.."/premake.lua")
        elseif os.isfile(TDir.."/premake5.lua") then
            dofile(TDir.."/premake5.lua")
        else
            print('Error Not premake.lua or premake5.lua TheDir: '..TDir)
        end
    end
end

function PmGenApp(the_name,apptype,charset,mfcbuildflags,staticcrt,winver)
    if g_the_pmver==3 then
        PmGenApp3(the_name,apptype,charset,mfcbuildflags,staticcrt,winver)
    end
    if g_the_pmver==5 then
        PmGenApp5(the_name,apptype,charset,mfcbuildflags,staticcrt,winver)
    end
end



function PmGenApp3(the_name,apptype,charset,mfcbuildflags,staticcrt,winver)
    if g_the_pmver~=3 then
        return
    end

    --print('load Gen App '.. the_name)


    --1. base1
    package.name     = the_name


    package.target   = the_name
    if g_dbg_suffix_d then
        package.target   = the_name..'.d'
    end

    package.language = "c++"
    if apptype=='WindowedApp' then
        package.kind     = "winexe"
    end
    if apptype=='ConsoleApp' then
        package.kind     = "exe"
    end
    if apptype=='SharedLib' then
        package.kind     = "dll"
    end
    if apptype=='StaticLib' then
        package.kind     = "lib"
    end

    if PmGenV6AppSlnRelativePjDir==nil then
        PmGenV6AppSlnRelativePjDir='..'
    end

    package.config["Release"].bindir = PmGenV6AppSlnRelativePjDir.."/Bin"..g_clpx.."/Release"
    package.config["Debug"].bindir = PmGenV6AppSlnRelativePjDir.."/Bin"..g_clpx.."/Debug"
    package.config["Release"].libdir = PmGenV6AppSlnRelativePjDir.."/Lib"..g_clpx.."/Release"
    package.config["Debug"].libdir = PmGenV6AppSlnRelativePjDir.."/Lib"..g_clpx.."/Debug"
    package.config["Release"].objdir = PmGenV6AppSlnRelativePjDir.."/Obj"..g_clpx.."/"..the_name.."/Release"
    package.config["Debug"].objdir = PmGenV6AppSlnRelativePjDir.."/Obj"..g_clpx.."/"..the_name.."/Debug"

    --1.1 base2
    table.insert( package.config["Debug"].defines, { "DEBUG", "_DEBUG" } )
    table.insert( package.config["Release"].defines, "NDEBUG" )
    package.config["Release"].buildflags = { "symbols", "optimize-speed","no-edit-and-continue"}


    --2 unicode mbcs
    if charset=='unicode' then
        table.insert( package.buildflags, "unicode" )
        table.insert(package.defines,{ "_UNICODE", "UNICODE" } )
    end

    if charset=='mbcs' then
        table.insert(package.defines,{ "_MBCS" } )
    end

    --3 mfc buildflags
    if mfcbuildflags=='MFC-Shared' or mfcbuildflags=='MFC-Shared-Ext' then
        if staticcrt~='ON' then
        table.insert(package.defines,{"_AFXDLL" } )
        end
        if mfcbuildflags=='MFC-Shared-Ext' then
        table.insert(package.defines,{"_AFXEXT" } )
        end
        table.insert(package.buildflags,"MFC-Shared" )
    end

    if mfcbuildflags=='MFC-Static' then
        table.insert(package.buildflags,"MFC-Static" )
    end

    --4 StaticRuntime
    if staticcrt=='ON' then
        table.insert( package.buildflags, "static-runtime" )
    end

    --5 defines
    table.insert(package.defines , {"_CRT_SECURE_NO_DEPRECATE"})
    table.insert(package.defines , {"WIN32"})

    if apptype=='WindowedApp' then
    table.insert(package.defines , {"_WINDOWS"})
    end
    if apptype=='ConsoleApp' then
        table.insert(package.defines , {"_CONSOLE"})
    end

    --6 winver
    if winver=="winxp" then
        table.insert(package.defines , {'_WIN32_WINNT=0x0501','WINVER=0x0501'})
    end
    if winver=="winvista" then
        table.insert(package.defines , {'_WIN32_WINNT=0x0600','WINVER=0x0600'})
    end
    if winver=="win7" then
        table.insert(package.defines , {'_WIN32_WINNT=0x0601','WINVER=0x0601'})
    end

    --7 include paths
    --if (add3rdInc~=nil) and (add3rdInc==true) then
    --table.insert(package.includepaths,{'./'} )
    --table.insert(package.includepaths,{g_the_top_dir} )
    --table.insert(package.includepaths,{g_the_top_dir..'/3rdparty'} )
    --end

    --8 links
    table.insert(package.links, {"version","Shell32","Rpcrt4","Gdi32","Kernel32","User32"} )

    --thelpD=g_the_top_dir.."\\Lib"..g_clpx..'\\Debug'
    --table.insert(package.config["Debug"].libpaths,{thelpD,"./"})
    --thelpR=g_the_top_dir.."\\Lib"..g_clpx..'\\Release'
    --table.insert(package.config["Release"].libpaths,{thelpR,"./"})

    --9 files
    PmGenV6AppPjAddFileSS={}
    if PmGenV6AppPjAddFile~=nil then
        table.insert(PmGenV6AppPjAddFileSS,matchfiles(PmGenV6AppPjAddFile))
    end
    if PmGenV6AppPjAddFileR~=nil then
        table.insert(PmGenV6AppPjAddFileSS,matchrecursive(PmGenV6AppPjAddFileR))
    end
    if PmGenV6AppPjAddFile2~=nil then
        table.insert(PmGenV6AppPjAddFileSS,matchfiles(PmGenV6AppPjAddFile2))
    end
    if PmGenV6AppPjAddFile2R~=nil then
        table.insert(PmGenV6AppPjAddFileSS,matchrecursive(PmGenV6AppPjAddFile2R))
    end

    package.files ={
        matchfiles('*.h','*.txt','*.c','*.cpp','*.cc','*.hpp','*.inl','*.def','*.rc','res/*','premake.lua'),
        PmGenV6AppPjAddFileSS
    }

    PmGenV6AppPjAddFile=nil
    PmGenV6AppPjAddFileR=nil
    PmGenV6AppPjAddFile2=nil
    PmGenV6AppPjAddFile2R=nil

    if PmFileIsExists('premake.lua') then
        os.rename('premake.lua','premake.lua')
    end

    --A
    if PmFileIsExists('StdAfx.h') and PmFileIsExists('StdAfx.cpp') then
        os.rename('stdafx.h','StdAfx.h')
        os.rename('stdafx.cpp','StdAfx.cpp')
        package.pchheader = "StdAfx.h"
        package.pchsource = "StdAfx.cpp"
    end
    if PmFileIsExists('stdAfx.h') and PmFileIsExists('stdAfx.cpp') then
        os.rename('stdafx.h','StdAfx.h')
        os.rename('stdafx.cpp','StdAfx.cpp')
        package.pchheader = "StdAfx.h"
        package.pchsource = "StdAfx.cpp"
    end

end


function PmGenApp5(the_name,apptype,charset,mfcbuildflags,staticcrt,winver)
if g_the_pmver~=5 then
    return
end
project (the_name)
    --1 base
    language 'C++'
    
	if g_buildsp_loction_buildir==true then
		location ('_Build'..g_clpx)
	else
		location ('./')
	end
		
    if apptype=='WindowedApp' then
        kind 'WindowedApp'
    end
    if apptype=='ConsoleApp' then
        kind 'ConsoleApp'
    end
    if apptype=='SharedLib' then
        kind 'SharedLib'
    end
    if apptype=='StaticLib' then
        kind 'StaticLib'
    end

    --2 define
    filter "platforms:Win32"
        architecture "x86"
        defines {'_WIN32'}
    filter "platforms:x64"
        architecture "x86_64"
        defines {'_WIN64'}
    filter {}

    if apptype=='WindowedApp' then
        defines {'_WINDOWS'}
        --defines {'_WIN32'}
    end

    if apptype=='ConsoleApp' then
        defines {'_CONSOLE'}
    end

    defines {"_CRT_SECURE_NO_WARNINGS"}

    --3 unicode mbcs
    if charset=='unicode' then
        if apptype=='WindowedApp' then
        linkoptions { "/ENTRY:wWinMainCRTStartup" }
        end
        if apptype=='ConsoleApp' then
            linkoptions { "/ENTRY:wmainCRTStartup" }
        end
        characterset ("Unicode")
        defines {'UNICODE','_UNICODE'}
    end

    if charset=='mbcs' then
        characterset ("MBCS")
        defines {'_MBCS'}
        if apptype=='WindowedApp' then
            linkoptions { "/ENTRY:WinMainCRTStartup" }
        end
    end

    --4 mfc buildflags
    if mfcbuildflags=='MFC-Shared' or mfcbuildflags=='MFC-Shared-Ext' then
        --#define _AFX     1
        --defines {'_AFX=1'}
        if staticcrt~='ON' then
            defines {"_AFXDLL" }
        end
        if mfcbuildflags=='MFC-Shared-Ext' then
            defines {"_AFXEXT" }
        end
        flags { "MFC" }
    end

    if mfcbuildflags=='MFC-Static' then
        --defines {"_AFX" }
        flags { "MFC" }
    end

    --5 StaticRuntime
    if staticcrt=='ON' then
        if g_vcver<10 then
            flags { "StaticRuntime" }
        end
        if g_vcver>=10 then
            staticruntime "On"
        end
    end

    --6 winver
    if winver=="winxp" then
        defines {'_WIN32_WINNT=0x0501','WINVER=0x0501'}
    end

    if winver=="winvista" then
        defines {'_WIN32_WINNT=0x0600','WINVER=0x0600'}
    end

    if winver=="win7" then
        defines {'_WIN32_WINNT=0x0601','WINVER=0x0601'}
    end



    if g_vsp_winxp==true then
        defines {'_WIN32_WINNT=0x0501','WINVER=0x0501'}
        if g_vcver==12 then
            toolset "v120_xp"
        end
        if g_vcver==14 then
            toolset "v140_xp"
        end
        if g_vcver>=15 then
            toolset "v141_xp"
        end
        if g_vcver>=14 then
        filter {"system:windows", "toolset:*_xp*"}
            buildoptions { "/Zc:threadSafeInit-" } -- Fix Windows XP not initialising TLS early
        filter{}
        end
    end


    --6.1 winver
    if (winver=="winxp") then
        if g_vcver==12 then
            toolset "v120_xp"
        end
        if g_vcver==14 then
            toolset "v140_xp"
        end
        if g_vcver>=15 then
            toolset "v141_xp"
        end

        if g_vcver>=14 then
            filter {"system:windows", "toolset:*_xp*"}
                buildoptions { "/Zc:threadSafeInit-" } -- Fix Windows XP not initialising TLS early
            filter{}
        end

        defines {'_WIN32_WINNT=0x0501'}
        defines {'WINVER=0x0501'}
        defines {'_CRT_NONSTDC_NO_DEPRECATE'}
        defines {'_CRT_SECURE_NO_WARNINGS'}
        defines {'_CRT_NON_CONFORMING_SWPRINTFS=1'}
        disablewarnings { "4131", "4244", "4996" }
    end


    --7 link
    libdirs { './'}

    filter {"platforms:Win32"}
        libdirs { g_the_top_dir.."/Lib"..g_clpx.."/%{cfg.buildcfg}"}
    filter {"platforms:x64"}
        libdirs { g_the_top_dir.."/Lib"..g_clpx.."/%{cfg.buildcfg}x64"}
    filter {}

    --8 file
    files {'*.h','*.txt','*.c','*.cpp','*.cc','*.hpp','*.inl','*.def','*.rc','res/*'}

    if os.isfile("premake.lua") then
         files {'premake.lua'}
    elseif os.isfile("premake5.lua") then
         files {'premake5.lua'}
    end
    vpaths { ['*'] = {path.translate(path.getabsolute("."), "\\")}}

    --9 pch
    if os.isfile('premake.lua')then
        os.rename('premake.lua','premake.lua')
    end
    if os.isfile('premake5.lua')then
        os.rename('premake5.lua','premake5.lua')
    end
    if os.isfile('StdAfx.h') and os.isfile('StdAfx.cpp') then
        os.rename('stdafx.h','StdAfx.h')
        os.rename('stdafx.cpp','StdAfx.cpp')
        pchheader ('StdAfx.h')
        pchsource ('StdAfx.cpp')
    end
end


function PmGenNone(the_name,tblfs)
project (the_name)
	if g_buildsp_loction_buildir==true then
		location ('_Build'..g_clpx)
	else
		location ('./')
	end
    kind 'None'
    --kind 'StaticLib'
    files (tblfs)
end

function PmGenDef(def)
    if g_the_pmver==3 then
        table.insert(package.defines,def)
    end
    if g_the_pmver==5 then
        defines {def}
    end
end

function PmGenInc(xinc)
    if g_the_pmver==3 then
        table.insert(package.includepaths,{xinc} )
    end
    if g_the_pmver==5 then
        includedirs {xinc}
    end
end

function PmGenFile(file)
    if g_the_pmver==3 then
        table.insert(package.files,matchfiles(file))
    end
    if g_the_pmver==5 then
        files {file}
    end
end


function PmGenLinkDp(libname)
    if g_the_pmver==5 then

        filter {"platforms:Win32"}
            filter {"configurations:Debug"}
                links{g_the_top_dir..'/Lib'..g_clpx..'/Debug/'..libname..'d.lib'}
            filter {"configurations:Release"}
                links{g_the_top_dir..'/Lib'..g_clpx..'/Release/'..libname..'.lib'}
            filter{}
        filter{}

        if g_the_top_w64 then
        filter {"platforms:x64"}
            filter {"configurations:Debug"}
                links{g_the_top_dir..'/Lib'..g_clpx..'/Debugx64/'..libname..'d.lib'}
            filter {"configurations:Release"}
                links{g_the_top_dir..'/Lib'..g_clpx..'/Releasex64/'..libname..'.lib'}
            filter{}
        filter {}
        end
    end

    if g_the_pmver==3 then
        if PmGenV6AppSlnRelativePjDir==nil then
            PmGenV6AppSlnRelativePjDir='..'
        end
        table.insert( package.config["Debug"].links, PmGenV6AppSlnRelativePjDir.."/Lib"..g_clpx.."/Debug/"..libname.."d"  )
        table.insert( package.config["Release"].links, PmGenV6AppSlnRelativePjDir.."/Lib"..g_clpx.."/Release/"..libname)
    end
end

function PmGenLink(libname)
    if g_dbg_suffix_d then
        PmGenLinkDp(libname)
        return
    end

    if g_the_pmver==5 then
        filter {"platforms:Win32"}
            links{g_the_top_dir..'/Lib'..g_clpx..'/%{cfg.buildcfg}/'..libname..'.lib'}
        filter {"platforms:x64"}
            links{g_the_top_dir..'/Lib'..g_clpx..'/%{cfg.buildcfg}x64/'..libname..'.lib'}
        filter {}
    end

    if g_the_pmver==3 then
        if PmGenV6AppSlnRelativePjDir==nil then
            PmGenV6AppSlnRelativePjDir='..'
        end
        table.insert( package.config["Debug"].links, PmGenV6AppSlnRelativePjDir.."/Lib"..g_clpx.."/Debug/"..libname )
        table.insert( package.config["Release"].links, PmGenV6AppSlnRelativePjDir.."/Lib"..g_clpx.."/Release/"..libname )
    end
end


function PmGenLinkDtp(libname)
    if g_the_pmver==5 then
        filter {"configurations:Debug"}
            links{libname..'d.lib'}
        filter {"configurations:Release"}
            links{libname..'.lib'}
        filter{}
        --links{libname}
    end

    if g_the_pmver==3 then
        --table.insert( package.links, libname )
        table.insert( package.config["Debug"].links, libname.."d"  )
        table.insert( package.config["Release"].links, libname)
    end
end


function PmGenLinkDt(libname)
    if g_dbg_suffix_d then
        PmGenLinkDtp(libname)
        return
    end

    if g_the_pmver==5 then
        links{libname}
    end

    if g_the_pmver==3 then
        table.insert( package.links, libname )
    end
end


function PmGenLinkDir(libdir)
    if g_the_pmver==5 then
        libdirs { libdir}
    end

    if g_the_pmver==3 then
        table.insert(package.config["Debug"].libpaths,{libdir})
        table.insert(package.config["Release"].libpaths,{libdir})
    end
end


function PmCopyFile(srcfile,srcdir,destdir)
    dest_with_file=''
    src_with_file=''
    destdirfx=''
    if g_the_pmver==3 then
        dest_with_file = destdir..'/'..srcfile, "/"
        src_with_file = srcdir..'/'..srcfile, "/"
        destdirfx = destdir
    end
    if g_the_pmver==5 then
        dest_with_file = path.translate(path.getabsolute(destdir..'/'..srcfile), "/")
        src_with_file = path.translate(path.getabsolute(srcdir..'/'..srcfile), "/")
        destdirfx = path.translate(path.getabsolute(destdir), "/")
    end
    if dest_with_file == src_with_file then
        return
    end
    os.mkdir(destdir)
    ok, err = os.copyfile(src_with_file,dest_with_file)
    if ok then
        --print('cp file '..srcfile..' from '..srcdir..' to '..destdirfx..'  ok...')
    else
        print('cp file '..srcfile..' from '..srcdir..' to '..destdirfx..'  fail 1...')
    end
end


function PmCopyFilesBin(fs,fsdir)
    if not fsdir then
      PmCopyFile(fs,".",g_the_top_dir..'/Bin'..g_clpx..'/Debug')
      PmCopyFile(fs,".",g_the_top_dir..'/Bin'..g_clpx..'/Release')
      if g_the_top_w64 then
          PmCopyFile(fs,".",g_the_top_dir..'/Bin'..g_clpx..'/Debugx64')
          PmCopyFile(fs,".",g_the_top_dir..'/Bin'..g_clpx..'/Releasex64')
      end
    else
      PmCopyFile(fs,fsdir,g_the_top_dir..'/Bin'..g_clpx..'/Debug')
      PmCopyFile(fs,fsdir,g_the_top_dir..'/Bin'..g_clpx..'/Release')
      if g_the_top_w64 then
          PmCopyFile(fs,fsdir,g_the_top_dir..'/Bin'..g_clpx..'/Debugx64')
          PmCopyFile(fs,fsdir,g_the_top_dir..'/Bin'..g_clpx..'/Releasex64')
      end
    end
end

function PmCopyFilesLib(fs,fsdir)
    if not fsdir then
      PmCopyFile(fs,".",g_the_top_dir..'/Lib'..g_clpx..'/Debug')
      PmCopyFile(fs,".",g_the_top_dir..'/Lib'..g_clpx..'/Release')
      if g_the_top_w64 then
          PmCopyFile(fs,".",g_the_top_dir..'/Lib'..g_clpx..'/Debugx64')
          PmCopyFile(fs,".",g_the_top_dir..'/Lib'..g_clpx..'/Releasex64')
      end
    else
      PmCopyFile(fs,fsdir,g_the_top_dir..'/Lib'..g_clpx..'/Debug')
      PmCopyFile(fs,fsdir,g_the_top_dir..'/Lib'..g_clpx..'/Release')
      if g_the_top_w64 then
          PmCopyFile(fs,fsdir,g_the_top_dir..'/Lib'..g_clpx..'/Debugx64')
          PmCopyFile(fs,fsdir,g_the_top_dir..'/Lib'..g_clpx..'/Releasex64')
      end
    end
end


--
--PmCopyFilesXF('kxlcr','dll','extlib/bin','Bin','WithPostD')
--fs        Name
--fs2       PosName  dll,lib
--fsdir     Name Source Dir
--todirf1   Name Target Bin,Lib
--flag      WithPostD or nil
--
function PmCopyFilesXF(fs,fs2,fsdir,todirf1,flag)
    fsdirfx=fsdir
    if not fsdir then fsdirfx="." end
    fsfx=fs..'.'..fs2
    if flag==nil then
        PmCopyFile(fsfx,fsdirfx,g_the_top_dir..'/'..todirf1..g_clpx..'/Debug');
        PmCopyFile(fsfx,fsdirfx,g_the_top_dir..'/'..todirf1..g_clpx..'/Release')
        if g_the_top_w64 then
        PmCopyFile(fsfx,fsdirfx,g_the_top_dir..'/'..todirf1..g_clpx..'/Debugx64')
        PmCopyFile(fsfx,fsdirfx,g_the_top_dir..'/'..todirf1..g_clpx..'/Releasex64')
        end
    end
    if flag=='WithPostD' then
        fsfx=fs..'d.'..fs2
        PmCopyFile(fsfx,fsdirfx,g_the_top_dir..'/'..todirf1..g_clpx..'/Debug');
        fsfx=fs..'.'..fs2
        PmCopyFile(fsfx,fsdirfx,g_the_top_dir..'/'..todirf1..g_clpx..'/Release')
        if g_the_top_w64 then
        fsfx=fs..'d.'..fs2
        PmCopyFile(fsfx,fsdirfx,g_the_top_dir..'/'..todirf1..g_clpx..'/Debugx64')
        fsfx=fs..'.'..fs2
        PmCopyFile(fsfx,fsdirfx,g_the_top_dir..'/'..todirf1..g_clpx..'/Releasex64')
        end
    end
end


function PmCopyFileIfNotExits(srcfile,srcdir,destdir)
    dest_with_file=''
    src_with_file=''
    destdirfx=''
    if g_the_pmver==3 then
        dest_with_file = destdir..'/'..srcfile, "/"
        src_with_file = srcdir..'/'..srcfile, "/"
        destdirfx = destdir
    end
    if g_the_pmver==5 then
        dest_with_file = path.translate(path.getabsolute(destdir..'/'..srcfile), "/")
        src_with_file = path.translate(path.getabsolute(srcdir..'/'..srcfile), "/")
        destdirfx = path.translate(path.getabsolute(destdir), "/")
    end
    if dest_with_file == src_with_file then
        return
    end
    os.mkdir(destdir)

    if PmFileIsExists(dest_with_file) then
    else
        ok, err = os.copyfile(src_with_file,dest_with_file)
        if ok then
            print('cp file '..srcfile..' from '..srcdir..' to '..destdirfx..'  ok...')
        else
            print('cp file '..srcfile..' from '..srcdir..' to '..destdirfx..'  fail 1...')
        end
    end
end

function PmCopyFilesBinIfNotExits(fs,fsdir,dstdiratt)
    if not fsdir then
      PmCopyFileIfNotExits(fs,".",g_the_top_dir..'/Bin'..g_clpx..'/Debug'..dstdiratt)
      PmCopyFileIfNotExits(fs,".",g_the_top_dir..'/Bin'..g_clpx..'/Release'..dstdiratt)
      if g_the_top_w64 then
          PmCopyFileIfNotExits(fs,".",g_the_top_dir..'/Bin'..g_clpx..'/Debugx64'..dstdiratt)
          PmCopyFileIfNotExits(fs,".",g_the_top_dir..'/Bin'..g_clpx..'/Releasex64'..dstdiratt)
      end
    else
      PmCopyFileIfNotExits(fs,fsdir,g_the_top_dir..'/Bin'..g_clpx..'/Debug'..dstdiratt);
      PmCopyFileIfNotExits(fs,fsdir,g_the_top_dir..'/Bin'..g_clpx..'/Release'..dstdiratt)
      if g_the_top_w64 then
          PmCopyFileIfNotExits(fs,fsdir,g_the_top_dir..'/Bin'..g_clpx..'/Debugx64'..dstdiratt)
          PmCopyFileIfNotExits(fs,fsdir,g_the_top_dir..'/Bin'..g_clpx..'/Releasex64'..dstdiratt)
      end
    end
end




